package com.virtualCat.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.virtualCat.common.Const;
import com.virtualCat.common.Msg;
import com.virtualCat.common.WeChatConfig;
import com.virtualCat.dao.CatDao;
import com.virtualCat.entity.Cat;
import com.virtualCat.service.CatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class CatServiceImpl implements CatService {
    @Autowired
    private CatDao catDao;

    /**
     * 获取猫咪的所有信息
     * @return
     */
    public Msg searchCatList(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Cat> catList=catDao.findAll();
        if (CollectionUtils.isEmpty(catList)){
            return Msg.success().add("null",10201);
        }
        if (catList!=null&&!catList.isEmpty()){
            for(Cat cat : catList){
                cat.setCimg(WeChatConfig.PIC_RESOURCES_PREFIX_PATH+cat.getCimg());
                cat.setGimg(WeChatConfig.PIC_RESOURCES_PREFIX_PATH+cat.getGimg());
            }
        }
        PageInfo pageInfo = new PageInfo(catList);
        return Msg.success().add("catlist",pageInfo);
    }

    /**
     * 获取猫咪信息
     * @param catId
     * @return
     */
    public Msg findCatInfo(Integer catId){
        Cat cat = catDao.findById(catId);
        if (cat==null){
            return Msg.fail().add("not exists",10202);
        }
        return Msg.success().add("catinfo",cat);
    }

    /**
     * 修改猫咪信息
     * @param cat
     * @return
     */
    public Msg updateCat(Cat cat){
        int rowCount = catDao.update(cat);
        if (rowCount == 0){
            return Msg.fail();
        }
        return Msg.success();
    }

    /**
     * 删除猫咪信息
     * @param catId
     * @return
     */
    public Msg deleteCat(Integer catId){
        int rowCount = catDao.delete(catId);
        if (rowCount == 0){
            return Msg.fail();
        }
        return Msg.success();
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    public String uploadFile(MultipartFile file,String flag){
        String path = Const.uploadPath;
        if (file != null) {
            String fileName = file.getOriginalFilename();
            String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);
            String uploadFileName = UUID.randomUUID().toString()+"."+fileExtensionName;
            String uploadPath=path+"cat/"+uploadFileName;
            if ("good".equals(flag)){
                uploadPath=path+"good/"+uploadFileName;
            }
            File localFile = new File(uploadPath);
            if (!localFile.exists()){
                localFile.mkdirs();
            }
            try {
                file.transferTo(localFile);
                return uploadFileName;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 多个上传图片
     * @param files
     * @return
     */
    public String[] uploadFiles(MultipartFile[] files){
        String path = Const.uploadPath;
        String[] newFileNames = new String[2];
        if (files.length>0 && files != null){
            for(int i= 0;i<files.length;i++){
                if (files[i] != null) {
                    String fileName = files[i].getOriginalFilename();
                    String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);
                    String uploadFileName = UUID.randomUUID().toString()+"."+fileExtensionName;
                    String uploadPath=path+"cat/"+uploadFileName;
                    if (i==1){
                        uploadPath=path+"good/"+uploadFileName;
                    }
                    File localFile = new File(uploadPath);
                    if (!localFile.exists()){
                        localFile.mkdirs();
                    }
                    try {
                        files[i].transferTo(localFile);
                        newFileNames[i] = uploadFileName;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return newFileNames;
    }

    /**
     * 添加猫咪
     * @param cat
     * @return
     */
    public Msg insertCat(Cat cat){
        int rowCount = catDao.insert(cat);
        if (rowCount == 0){
            return Msg.fail();
        }
        return Msg.success();
    }
}
